const express = require('express');
const router = express.Router();
const SQLConnect = require('./SQLConnect')
const jwt = require('jsonwebtoken')
const config = require("./config")
const url = require('url')
const multer = require("multer");
const fs = require("fs")

//登录
router.post('/login', (req, res) => {
    const phone = req.body.phone
    const password = req.body.password
    const sql = "select * from login where phone=? and password=?";
    var arr = [phone, password]

    SQLConnect(sql, arr, (result) => {
        if (result.length > 0) {

            //生成token字段
            const token = jwt.sign({
                phone: phone,
                password: password
            }, config.jwtSecret)

            res.send({
                status: 200,
                data: result,
                msg: '登录成功',
                token: token
            })
        } else {
            res.send({
                status: '500',
                msg: '用户名密码错误'
            })
        }
    })
})

// 注册新人
router.post('/signnew',(req,res)=>{
    let username= req.body.username
    let password= req.body.password
    let phone= req.body.phone
    let email= req.body.email
    let wedding= req.body.wedding
    let identification= '1'
    const sql = "select * from login where phone=? and email=?";
    var arr = [phone, email]
    SQLConnect(sql,arr,(result) => {
        if(result.length>0){
            res.send({
                msg:'此用户已被注册',
                status:400
            })
        }else{
            const sqlzhuce = `insert into login (username,password,phone,email,wedding,new) values ('${username}','${password}','${phone}','${email}','${wedding}','${identification}')`;
            const arrs =[];
            SQLConnect(sqlzhuce,arrs,(results) => {
                if(results.affectedRows){
                    res.send({
                        status:200,
                        msg:"success"
                    })
                }else{
                    res.send({
                        status:400,
                        msg:"注册失败"
                    })
                }
            })
        }
    })
})
// 注册公司内部人员
router.post('/signodd',(req,res)=>{
    let username= req.body.username
    let password= req.body.password
    let phone= req.body.phone
    let email= req.body.email
    let gongsi= req.body.gongsi
    let identification= '2'
    const sql = "select * from login where phone=? and email=?";
    var arr = [phone, email]
    SQLConnect(sql,arr,(result) => {
        if(result.length>0){
            res.send({
                msg:'此用户已被注册',
                status:400
            })
        }else{
            const sqlzhuce = `insert into login (username,password,phone,email,gongsi,new) values ('${username}','${password}','${phone}','${email}','${gongsi}','${identification}')`;
            const arrs =[];
            SQLConnect(sqlzhuce,arrs,(results) => {
                if(results.affectedRows){
                    res.send({
                        status:200,
                        msg:"success"
                    })
                }else{
                    res.send({
                        status:400,
                        msg:"注册失败"
                    })
                }
            })
        }
    })
})
// 修改密码接口
router.post('/changes', (req, res) => {
    const phone = req.body.phone
    let email= req.body.email
   
    const sql = "select * from login where phone=? and email=?";
    var arr = [phone, email]

    SQLConnect(sql, arr, (result) => {

        if (result.length > 0) {

            res.send({
                status: 200,
                data: result,
                msg: '密码可以修改',
            })
           
        } else {
            res.send({
                status: '500',
                data: result,
                msg: '手机或邮箱不正确'
            })
        }
    })
})
router.post('/changepwd', (req, res) => {
    const phone = req.body.phone
    let email= req.body.email
    let password= req.body.password
    const  change = `update login set password='${password}'where phone='${phone}' and email='${email}'`;
    var arr = []

    SQLConnect(change, arr, (result) => {
        if(result.affectedRows>0){
            // 可以找到
            res.send({
                msg:'success',
                
            })
        }else{
            // 手机或者邮箱找不到
            res.send({
                msg:'手机或者邮箱找不到',
                status:"500"
            })
        }
      
    })
})

// 所有主持人
router.get('/host',(req,res)=>{
    const sql="select * from host";
    SQLConnect(sql,null,result=>{
        if(result.length>0){
            res.send({
                status:200,
                data:{
                    result
                }
            })
        }else{
            res.send({
                status:500,
                msg:'暂无数据'
            })
        }
    })
})               
// 策划师注册
router.post('/cehuashi',(req,res)=>{
    let username= req.body.username //策划师名字
    let password= req.body.password //密码
    let phone= req.body.phone  //手机
    let email= req.body.email  //邮箱
    let identification= '3'
    const sql = "select * from login where phone=? and email=?";
    var arr = [phone, email]
    SQLConnect(sql,arr,(result) => {
        if(result.length>0){
            res.send({
                msg:'此用户已被注册',
                status:400
            })
        }else{
            const sqlzhuce = `insert into login (username,password,phone,email,new) values ('${username}','${password}','${phone}','${email}','${identification}')`;
            const arrs =[];
            SQLConnect(sqlzhuce,arrs,(results) => {
                if(results.affectedRows){
                    res.send({
                        status:200,
                        msg:"success"
                    })
                }else{
                    res.send({
                        status:400,
                        msg:"注册失败"
                    })
                }
            })
        }
    })
})
// /主持人注册
router.post('/zhuchiren',(req,res)=>{
    let username= req.body.username //主持人名字
    let password= req.body.password //密码
    let phone= req.body.phone  //手机
    let email= req.body.email  //邮箱
    let identification= '4'
    const sql = "select * from login where phone=? and email=?";
    var arr = [phone, email]
    SQLConnect(sql,arr,(result) => {
        if(result.length>0){
            res.send({
                msg:'此用户已被注册',
                status:400
            })
        }else{
            const sqlzhuce = `insert into login (username,password,phone,email,new) values ('${username}','${password}','${phone}','${email}','${identification}')`;
            const arrs =[];
            SQLConnect(sqlzhuce,arrs,(results) => {
                if(results.affectedRows){
                    res.send({
                        status:200,
                        msg:"success"
                    })
                }else{
                    res.send({
                        status:400,
                        msg:"注册失败"
                    })
                }
            })
        }
    })
})





module.exports = router;